<template>
    <div class="a-tree">
        <template v-for="(a, i) in items" :key="getKeyFunc(a)">
            <template v-if="getIsFolderFunc(a)">
                <slot
                    name="foldName"
                    :item="a"
                    :parent="parent"
                    :depth="depth"
                    :index="i"
                    :itemCount="items.length"
                    :opened="getIsItemOpenedFunc(a)"
                ></slot>
                <a-tree
                    :items="getItemsFunc(a)"
                    :getItemsFunc="getItemsFunc"
                    :parent="a"
                    :getKeyFunc="getKeyFunc"
                    :getIsFolderFunc="getIsFolderFunc"
                    :getIsItemOpenedFunc="getIsItemOpenedFunc"
                    :depth="depth + 1"
                    v-if="getIsItemOpenedFunc(a)"
                >
                    <template
                        v-slot:foldName="{
                            item,
                            parent,
                            depth,
                            index,
                            itemCount,
                            opened,
                        }"
                    >
                        <slot
                            name="foldName"
                            :item="item"
                            :parent="parent"
                            :depth="depth"
                            :index="index"
                            :itemCount="itemCount"
                            :opened="opened"
                        ></slot>
                    </template>
                    <template
                        v-slot:file="{ item, parent, depth, index, itemCount }"
                    >
                        <slot
                            name="file"
                            :item="item"
                            :parent="parent"
                            :depth="depth"
                            :index="index"
                            :itemCount="itemCount"
                        ></slot>
                    </template>
                </a-tree>
            </template>
            <template v-else>
                <slot
                    name="file"
                    :item="a"
                    :parent="parent"
                    :depth="depth"
                    :index="i"
                    :itemCount="items.length"
                ></slot>
            </template>
        </template>
    </div>
</template>

<script>
export default {
    name: 'a-tree',
    props: {
        items: {
            type: Array,
            default: []
        },
        getItemsFunc: {
            type: Function,
            default: a => a.items,
        },
        parent: {
            type: Object,
            default: null,
        },
        getKeyFunc: {
            type: Function,
            default: a => a.id,
        },
        getIsFolderFunc: {
            type: Function,
            default: a => a.type == 0,
        },
        getIsItemOpenedFunc: {
            type: Function,
            default: a => a.on == true,
        },
        depth: {
            type: Number,
            default: 0
        }
    },
}
</script>